<?php
/**
 * 装修材料服务
 * 
 * @author river_he@eueang.com
 */
include_once(AMFPHP_BASE . "shared/util/MethodTable.php");
final class DecorateService extends Kit_Service{
	/**
	 * 获取材料列表
	 * 
	 * @param int $postion 装修位置
	 * @param int $page 页码
	 * @param int $psize 页记录数
	 * @return array
	 */
	public function getDecorates($postion, $page, $psize){
		$page = $page>0?$page:1;
		$psize = $psize>0?$psize:10;
		$key = __FUNCTION__.'_'.$postion.'_'.$page.'_'.$psize;
		$key = $this->getCacheKey($key);
		$decorates = false; // $this->getMemcache()->get($key);
		if($decorates === false){
			$where = array('nOnShelf=1');
			if($postion == 1){
				//@todo for test
			}elseif($postion == 2){
				array_push($where, 'nPosit BETWEEN 3 AND 4');
			}elseif($postion == 3){
				array_push($where, 'nPosit=5');
			}elseif($postion == 4){
				array_push($where, 'nPosit BETWEEN 6 AND 7');
			}
			$decorates = $this->getDecoMode()->getItems($where, 'nHeat ASC', $page, $psize);
			if(!$decorates){
				return $this->pack(201, 'page not found');
			}
			//$this->getMemcache()->add($key, $decorates, 10);
		}
		return $this->pack(200, 'OK', $decorates);
	}
	
	/**
	 * 取消购买装修家居
	 * 
	 * @param int $shoperId
	 * @param int $shopId
	 * @param int $shopDecoId
	 * @return array
	 */
	public function sell($shoperId, $shopDecoId){
		//@todo 继续完善
		if(1 > $shoperId){
			return $this->pack(101, 'argument shoperid error');
		}elseif (1 > $shopDecoId){
			return $this->pack(102, 'argument shopDecoId error');
		}
		$sell = $this->getDecoMode()->sell($shoperId, $shopDecoId);
		if($sell == 1){
			return $this->pack(200, 'OK');
		}else{
			$code = 0;
			$message = '';
			switch($sell){
				case -1:
					$code += 201;
					$message .= 'no decorate';
					break;
				case -2:
					$code += 202;
					$message .= 'no shper';
					break;
				case -3:
					$code += 203;
					$message .= '该家具已经使用过';
					break;
				case -4:
					$code += 204;
					$message .= 'no shoper';
					break;
				case -5:
					$code += 205;
					$message .= 'operation error';
					break;
				case -6:
					$code += 206;
					$message .= 'server error';
					break;
			}
			return $this->pack($code, $message);
		}
		return $this->pack(501, 'server error');
	}
	
	/**
	 * 购买装修家居
	 * 
	 * @param int $shoperId 店主id
	 * @param int $decoId 家居id
	 * @param int $total 购买数量
	 * @return array
	 */
	public function buy($shoperId, $decoId, $total){
		if(1 > $shoperId){
			return $this->pack(101, 'argument shoperId error');
		}elseif(1 > $decoId){
			return $this->pack(102, 'argument decoId error');
		}elseif(1 > $total){
			return $this->pack(103, 'argument total error');
		}
		$buy = $this->getDecoMode()->buy($shoperId, $decoId, $total);
		if(is_array($buy)){
			return $this->pack(200, 'OK', $buy);
		}else{
			$message = '';
			$code = 0;
			switch($buy){
				case -1:
					$message .= 'decorate not found';
					$code = 201;
					break;
				case -2:
					$message .= 'shoper not found';
					$code = 202;
					break;
				case -3:
					$message .= '装修材料定价错误';
					$code = 205;
					break;
				case -4:
					$message .= '金币不足';
					$code = 206;
					break;
				case -5:
					$message .= '美食币不足';
					$code = 207;
					break;
				case -6:
					$message .= '数据处理错误';
					$code = 208;
					break;
				case -7:
					$message .= '数据处理错误';
					$code = 209;
					break;
			}
			return $this->pack($code, $message);
		}
	}
}
//end